package ro.theneckmaster.usbSync;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.SwingWorker;

import ro.theneckmaster.usbSync.SyncronizeActionModel.FileAction;
import ro.theneckmaster.usbSync.gui.MainFrame;

public class FolderSyncAnalize extends SwingWorker<Void, Void> {

	private MainFrame mainFrame;
	private String message;
	
	private List<SyncronizeActionModel> actionQueue;
	private int counter;
	
	@Override
	protected Void doInBackground() throws Exception {
		// TODO Auto-generated method stub
		
		setProgress(0);
		setMessage("Analizing Folders...");
		mainFrame.setFolderLocal(new Folder(mainFrame.getTextFieldLocalFolder().getText(), ""));
		mainFrame.setFolderRemote(new Folder(mainFrame.getTextFieldRemoteFolder().getText(), ""));
		setMessage("Done\n");
		
		setMessage("Setting Up Action Queue...");
		actionQueue = new ArrayList<SyncronizeActionModel>();
		counter = 0;
		parseFoldersRecursive(mainFrame.getFolderLocal(), mainFrame.getFolderRemote().getPath(), mainFrame.getRadioButtonLocal().isSelected(), mainFrame.getRadioButtonRemote().isSelected());
		parseFoldersRecursive(mainFrame.getFolderRemote(), mainFrame.getFolderLocal().getPath(), mainFrame.getRadioButtonRemote().isSelected(), mainFrame.getRadioButtonLocal().isSelected());
		setMessage("Done");

		// this is where all the action is taken
		/*
		for (Iterator<SyncronizeActionModel> iterator = actionQueue.iterator(); iterator.hasNext();) {
			SyncronizeActionModel actionModel = (SyncronizeActionModel) iterator.next();
			setMessage(actionModel.getActionMessage());
			actionModel.executeAction();
			setProgress(Math.min((int)Math.round((double)actionModel.getId()/actionQueue.size()*100), 100));
		}
		setMessage("Done");
		*/
		this.firePropertyChange("actionQueue", null, actionQueue);
		mainFrame.doneLoadingFolders();

		return null;
	}


	public MainFrame getMainFrame() {
		return mainFrame;
	}


	public void setMainFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	// de facut sa numere si sa faca alta functie treaba mai incolo....
	public void parseFoldersRecursive(Folder fromFolder, String relativePath, boolean deleteFrom, boolean deleteTo) {
		File fromFolderObj = new File(fromFolder.getPath());
		File toFolderObj = new File(relativePath+fromFolder.getRelativePath());
		
		if (!toFolderObj.exists()) {
			if (deleteFrom) {
				actionQueue.add(new SyncronizeActionModel(counter++, FileAction.DELETE_FOLDER, fromFolderObj.getAbsolutePath(), null));
			} else {
				actionQueue.add(new SyncronizeActionModel(counter++, FileAction.CREATE_FOLDER, null, toFolderObj.getAbsolutePath()));
			}
		}
		
		for (Iterator<Folder> iterator = fromFolder.getSubFolders().iterator(); iterator.hasNext();) {
			Folder subFolder = (Folder) iterator.next();
			parseFoldersRecursive(subFolder, relativePath, deleteFrom, deleteTo);
		}
		
		for (Iterator<MyFile> iterator = fromFolder.getFiles().iterator(); iterator.hasNext();) {
			MyFile file = (MyFile) iterator.next();
			File fromFileObj = new File(file.getPath());
			File toFileObj = new File(relativePath+file.getRelativePath());
			if (!toFileObj.exists()) {
				if (deleteFrom) {
					actionQueue.add(new SyncronizeActionModel(counter++, FileAction.DELETE_FILE, fromFileObj.getAbsolutePath(), null));
				} else {
					actionQueue.add(new SyncronizeActionModel(counter++, FileAction.COPY_FILE, fromFileObj.getAbsolutePath(), toFileObj.getAbsolutePath()));
				}
			} else {
				if (fromFileObj.lastModified() > toFileObj.lastModified()) {
					actionQueue.add(new SyncronizeActionModel(counter++, FileAction.REPLACE_FILE, fromFileObj.getAbsolutePath(), toFileObj.getAbsolutePath()));
				}
			}
		}
		
	}


	public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		this.firePropertyChange("message", this.message, message);
		this.message = message;
	}

}
